package com.liuceng.zen.web.auth;

import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AutoLoginMatcher extends SimpleCredentialsMatcher {
  private final static Logger LOG = LoggerFactory.getLogger(AutoLoginMatcher.class);

  private String allowReferer;

  public AutoLoginMatcher(String allowReferer) {
    this.allowReferer = allowReferer;
  }

  @Override
  public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
    UsernamePasswordToken atoken = (UsernamePasswordToken) token;
    String username = atoken.getUsername();
    String password = String.valueOf(atoken.getPassword());
    //
    if (atoken.getUsername() != null) {
      try {
        Integer memberId = Integer.parseInt(username);
      } catch (Exception e) {
        LOG.error("username not allow " + username);
        return false;
      }
      if (!password.contains(allowReferer)) {
        LOG.error("referer not allow " + password);
        return false;
      }
      return true;
    }
    return false;
  }

  public String getAllowReferer() {
    return allowReferer;
  }

  public void setAllowReferer(String allowReferer) {
    this.allowReferer = allowReferer;
  }
}
